package com.jfreer.mybatis.expand.javamapper.elements;

import com.jfreer.mybatis.expand.IntrospectedTableMyBatis3Freework;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.AbstractJavaMapperMethodGenerator;

import java.util.Set;
import java.util.TreeSet;

/**
 * Created by landy on 2017/12/10.
 */
public class UpdateByPrimaryKeySelectiveInBatchMethodGenerator extends
        AbstractJavaMapperMethodGenerator {

    public UpdateByPrimaryKeySelectiveInBatchMethodGenerator() {
        super();
    }

    @Override
    public void addInterfaceElements(Interface interfaze) {
        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
        FullyQualifiedJavaType parameterType;

        if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
            parameterType = new FullyQualifiedJavaType(introspectedTable
                    .getRecordWithBLOBsType());
        } else {
            parameterType = new FullyQualifiedJavaType(introspectedTable
                    .getBaseRecordType());
        }

        importedTypes.add(parameterType);

        Method method = new Method();
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setReturnType(FullyQualifiedJavaType.getIntInstance());
        method.setName(((IntrospectedTableMyBatis3Freework) introspectedTable)
                .getUpdateByPrimaryKeySelectiveInBatchStatementId());
        method.addParameter(new Parameter(parameterType, "record","@Param(\"record\")")); //$NON-NLS-1$

        /* ############################ */
        FullyQualifiedJavaType idsParameterType = FullyQualifiedJavaType
                .getNewListInstance();
        FullyQualifiedJavaType pkType;
        pkType = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType();

        importedTypes.add(pkType);
        idsParameterType.addTypeArgument(pkType);

        method.addParameter(new Parameter(idsParameterType, "ids","@Param(\"list\")")); //$NON-NLS-1$

        importedTypes.add(new FullyQualifiedJavaType(
                "org.apache.ibatis.annotations.Param")); //$NON-NLS-1$
        /* ############################ */

        context.getCommentGenerator().addGeneralMethodComment(method,
                introspectedTable);

        addMapperAnnotations(interfaze, method);

        if (context.getPlugins()
                .clientUpdateByPrimaryKeySelectiveMethodGenerated(method,
                        interfaze, introspectedTable)) {
            interfaze.addImportedTypes(importedTypes);
            interfaze.addMethod(method);
        }
    }

    public void addMapperAnnotations(Interface interfaze, Method method) {
        return;
    }
}